package zbl.c0.main;

/**
 * @author ZhuBolong
 * 	 该类是整个编译程序的唯一主入口
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import zbl.c0.lexer.Lexer;
import zbl.c0.parser.Parser;

public class MainEntrance {
	
	private static String[] runArgs;   // 记录用户的输入信息，作为当前程序的命令变量
		
	/**
	 * Display Help Information...
	 * @param args
	 */
	static void printManInfo(){
		System.out.println("  -man		display help information");
		System.out.println("  c0cc filename.c0	use c0 compiler compile filename.c0");
		System.out.println("  about		display the information about this programm");
		System.out.println("  quit		exit this programm");
	}
	
	/**
	 * Display Information about this software
	 * @param args
	 */
	static void printAboutInfo(){
		System.out.println("	This programm is a C0Compiler ,which can compile a sourcefile written " +'\n'+
				"in C0 language. It produce four factor expression as the intermediate code and finally " +'\n'+
				"generate assemble code after some optimization. It was written by ZhuBolong as a term "+'\n'+
				"project for Compiler Course in BUAA. I am very glad to share exprience with you!");
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println(">>>MainEntrance has been started!  Welcome!!!");
		System.out.println(">>>To get more information ,you can input -man");
		
		
		try{
			while(true){
				System.out.print(">>>");
				
				BufferedReader in = new BufferedReader(
										new InputStreamReader(System.in));
	
				String str = in.readLine();
				runArgs = str.split(" ");
				
				if(runArgs[0].equals("-man")){
					printManInfo();
				}
				else if(runArgs[0].equals("about")){
					printAboutInfo();
				}
				else if(runArgs[0].equals("quit"))
				{
					System.out.println("Bye~~	Bye~~");
					in.close();
					System.exit(0);
				}
				else if(runArgs[0].equals("c0cc")){
					try{
						if(!(runArgs[1].substring(runArgs[1].length()-3, runArgs[1].length())).equals(".c0")){
							
							System.out.println("File formation errors, you should input a filename as *.c0!");
							
						}else{
//#####################################################################################################################
							
							Lexer lexer = new Lexer();
							
							lexer.accessFile(runArgs[1]);			//  词法分析器加载源程序
							
							Parser myParser = new Parser(lexer);
							myParser.Program();
							
							System.out.println();
							System.out.println("******************The End!!!******************");
							
		
//#####################################################################################################################
						}
					}
					catch(java.lang.ArrayIndexOutOfBoundsException eArrayOut){
						System.out.println("Invalid Instruction!!!");
						System.out.println("Please add a filename after c0cc as an argument!!!");
					}
					catch(java.io.FileNotFoundException eFileFound){
						System.out.println("Can't find "+runArgs[1]+ " in default directory!");
						System.out.println("You should ensure this file is in the same " +'\n'+
								"directory as the root directory of this programm!");
					}
				}
				else{
					System.out.println(">>>Invalid Instruction!!!");
					System.out.println(">>>To get more information ,you can input -man");
				}

			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
			
	}
	
}
